﻿using System;
using System.Web.UI.WebControls;
using SocialShare.BusinessLogicLayer;
using SocialShare.BusinessEntities;
using SocialShare.DAL.Facades;
using SocialShare.BLL;
using System.Configuration;

public partial class Admin_Add_User : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
         
    protected void lnbSave_Click(object sender, EventArgs e)
    {
        ConfigReader configReader = new ConfigReader();
        MemberFacade memberFacade = new MemberFacade();

        SocialShare_Members member = new SocialShare_Members();
        member.CreateDate = DateTime.Now;
        member.Email = txtEmailAddress.Text.Trim();
        member.Password = txtPassword.Text.Trim();
        member.ScreenName = txtScreenName.Text.Trim();
        member.AuthCode = Guid.NewGuid().ToString();
        member.AccountStatus = (Int32)MEMBER_STATUS.Active;

        SocialShare_MemberProfiles profile = new SocialShare_MemberProfiles();
        profile.BlogName = txtBlogName.Text.Trim();
        profile.DOB= Convert.ToDateTime(txtDOB.Text.Trim());
        profile.First_Name = txtFirstName.Text.Trim();
        profile.Last_Name = txtlastName.Text.Trim();
        profile.Website = txtWebSite.Text.Trim();
        profile.Sex = rdblSex.SelectedItem.Value;

        if (memberFacade.GetMemberByScreenName(txtScreenName.Text.Trim()) != null)
        {
            divMessage.Attributes.Add("class", "error");
            lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.SCREEN_NAME_EXISTS.ToString());

        }

        if (memberFacade.GetMemberByUserName(txtEmailAddress.Text.Trim()) != null)
        {
            divMessage.Attributes.Add("class", "error");
            lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.EMAIL_EXISTS.ToString());

        }
        else
        {
            Int32 memberId = memberFacade.CreateNewMember(member);
            if (memberId > 0)
            {
                profile.UserId = memberId.ToString();
                memberFacade.CreateNewMemberProfile(profile);
                divMessage.Attributes.Add("class", "success");
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.REGISTRATION_SUCCESS.ToString());
            }
            else
            {
                divMessage.Attributes.Add("class", "error");
                lblMessage.Text = configReader.ReadConfigurationValue(ConfigurationManager.AppSettings[APP_SETTINGS_NAME.MessageLocation.ToString()], SITE_MESSAGES.REGISTRATION_FAILED.ToString());
            }
        }
    }
}
